require "proto/noticerank/message/ReqChangeAndWishRewardMessage"
require "proto/noticerank/message/ReqSynRankNoticMessage"
require "proto/noticerank/message/ReqSelfAwardInfoMessage"
require "proto/noticerank/message/ReqSelfRankInfoMessage"
require "proto/noticerank/message/ReqMainRankInfoMessage"
require "proto/noticerank/message/ReqReceiveSelfAwardMessage"
require "proto/noticerank/message/ReqEditMsgMessage"
require "proto/noticerank/message/ReqGivePraiseMessage"
require "proto/noticerank/message/ReqRTFightRankMessage"
require "proto/seven_rank/message/ReqSevenRankListMessage"
require "proto/seven_rank/message/ReqSevenRankGetLimitRewardMessage"
require "proto/seven_rank/message/ReqSevenRankSignRewardMessage"
require "proto/seven_rank/message/ReqSevenRank2ListMessage"
require "proto/seven_rank/message/ReqSevenRankGetStepRewardMessage"

NoticeHandler = {}
local t = NoticeHandler
--公告板
function t.ReqSynRankNotice()
    sendReqMessage(ReqSynRankNoticMessage.New())
end
--祈福
function t.ReqRankWish()
    --sendReqMessage(ReqChangeAndWishRewardMessage.New())
    -- 屏蔽祈福
end
--开服排行榜排行奖励查询
function t.ReqSelfAwardRedPoint()
    --sendReqMessage(ReqSelfAwardInfoMessage.New())
end
--领取异动奖励(协议已废弃)
-- function t.ReqChangeAwardByType(type)
--     local data = logicMgr.NoticeManager.changeAwardList[type]
--     if data == nil then return end
--     local m = ReqChangeAwardMessage.New()
--     m.changeAwardId = data.cfgId
--     sendReqMessage(m)
-- end

--发送编辑雕像战力榜宣言
function t.ReqEditMsg(msg)
    local m = ReqEditMsgMessage.New()
    m.msg = msg
    sendReqMessage(m)
end
--发送点赞雕像战力榜玩家
function t.ReqGivePraise(playerId)
    local m = ReqGivePraiseMessage.New()
    m.playerId = playerId
    sendReqMessage(m)

end
--发送雕像即时战力榜
function t.ReqRTFightRank(msg)
    sendReqMessage(ReqRTFightRankMessage.New())
end
--自身排行榜
function t.ReqSelfRankInfo(bigType,subType)
    local m = ReqSelfRankInfoMessage.New()
    m.rankType = bigType
    m.rankSubType = subType
    sendReqMessage(m)
end
--总榜
function t.ReqMainRankInfo(type,callback)
    local m = ReqMainRankInfoMessage.New()
    m.rankType = type
    sendReqMessage(m)
    t.reqMainRankInfoCallback = callback
end
--请求七日榜分榜领取奖励
function t.ReqReceiveSelfAward(rankType)
    local m = ReqReceiveSelfAwardMessage.New()
    m.rankType = rankType
    sendReqMessage(m)
end
--
function t.ReqRedDots(billboard,rank)
    if billboard then
        t.ReqSynRankNotice()
    end
    if rank then
        t.ReqSelfAwardRedPoint()
    end
    logicMgr.NoticeManager.RedDotsDelayCheckReq()
end
--公告排行信息同步
function t.ResSynRankNoticHandler(msg)
    if msg.state ~= 0 then return end
    local mgr = logicMgr.NoticeManager
    mgr.Clear()
    mgr.buffId = msg.buffId
    mgr.openTime = tonumber(tostring(msg.openTime))
    mgr.endTime = tonumber(tostring(msg.endTime))
    print("billboard begin:"..os.date("%Y-%m-%d %H:%M:%S",mgr.openTime)
            .."  end:"..os.date("%Y-%m-%d %H:%M:%S",mgr.endTime)
            .."  cur:"..os.date("%Y-%m-%d %H:%M:%S",HelperFunc.GetServerTime()))
    for k,v in pairs(msg.changeAward) do
        logicMgr.NoticeManager.AddAlertChange(v)
    end
    --add normal tip
    logicMgr.NoticeManager.TryAddAlertNormalChange()
    for k,v in pairs(msg.playerList) do
        logicMgr.NoticeManager.AddAlertPlayer(v)
    end
    if HelperFunc.GetServerTime() < logicMgr.NoticeManager.openTime then
    --    UISysTips.AddMsg("未到开放时间")
    --    log("未到开放时间："..os.date("%Y年%m月%d日 %H:%M:%S",logicMgr.NoticeManager.openTime))
   --     return
    end
    FireEvent(Config.EventType.BillboardUpdate)
    --showUI(PanelNames.UISystemBillBoard,t.pageIndex)
    --刷新红点
    t.ReqSelfAwardRedPoint()
end
--请求祈福与异动奖励
function t.ResChangeAndWishRewardHandler(msg)
    if msg.state ~= 0 then return end
    if msg.buffId > 0 then
        logicMgr.NoticeManager.buffId = msg.buffId
    end
    local rewardList = {}
    local rewardInfo = msg.changeAward
    for k,info in pairs(rewardInfo) do
        local cfg = dataTable.getDataTableById("t_noticeranklist",info.cfgId)
        if cfg then
            local d = logicMgr.NoticeManager.changeAwardList[cfg.f_RankType]
            if d then
                d.state = 1
            end
            local data = {type = cfg.f_RankType,data = logicMgr.ItemManager.ParseRewards(cfg.f_Rewards)}
            table.insert(rewardList,data)
        end
    end
    if msg.buffId > 0 or #rewardList > 0 then
        showUI(PanelNames.UIBillboardReward,rewardList)
    end
    FireEvent(Config.EventType.BillboardRewardRedPoint)
    FireEvent(Config.EventType.RefreshNoticeBless)
end
--战力、消耗、社交榜分榜
--返回编辑雕像战力榜宣言
function t.ResEditMsgHandler(msg)
    if msg.state ~= 0 then return end
    FireEvent(Config.EventType.RefreshSpeak,msg.msg)

end

--返回点赞雕像战力榜玩家
function t.ResGivePraiseHandler(msg)
    if msg.state == 0 then
        logicMgr.NoticeManager.SetPlayerGive(msg.playerId);
        logicMgr.UIShowManager.ShowRewardByTempItemInfo(msg.award)
        logicMgr.NoticeManager.RefreshSumClick()
        FireEvent(Config.EventType.RefreGivePrais,msg.playerId)
    end
end
--返回雕像即时战力榜
function t.ResRTFightRankHandler(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.SetRankPlayList(msg);
    FireEvent(Config.EventType.RefreshRankList)    
end
--返回自身所在子榜的排行信息
function t.ResSelfRankInfoHandler(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.SetMyRank( msg.selfInfo,msg.myRank,msg.rankType,msg.rankSubType)
    logicMgr.NoticeManager.SetRanks(msg.playerList)
    logicMgr.NoticeManager.SetMyRewards(msg.rankType,msg.selfAwardInfo)
    logicMgr.NoticeManager.SetSubRankRewardInfo(msg.rankType,msg.onSubRankRewardList)
    FireEvent(Config.EventType.RefreshNoticeSelfRank)
end
--总榜（目前只用社交榜在用了）
function t.ResMainRankInfoHandler(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.SetMainRanks(msg)
    FireEvent(Config.EventType.RefreshNoticeMainRank,msg.rankType)
    if t.reqMainRankInfoCallback then
        t.reqMainRankInfoCallback()
        t.reqMainRankInfoCallback = nil
    end
end
--领取自身开服榜奖励
function t.ResReceiveSelfAwardHandler(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.SetMyRewardState(msg.rankType,1)
    FireEvent(Config.EventType.RefreshNoticeGetRankReward)
    t.ReqSelfAwardRedPoint()
end
--查询开服排行榜奖励（红点用）
function t.ResSelfAwardInfoHandler(msg)
    if msg.state ~= 0 then return end
    table.clear(logicMgr.NoticeManager.rewardRedPointList)
    for k,v in pairs(msg.rankTypes) do
        logicMgr.NoticeManager.rewardRedPointList[v] = true
    end
    FireEvent(Config.EventType.BillboardRewardRedPoint)
end
--分榜上榜奖励通知
function t.ResOnSubRankRewardNoticeHandler(msg)
    if msg.state ~= 0 then return end
    local rewards = {}
    for k,v in pairs(msg.list) do
        local cfg = dataTable.getDataTableByMultiId("t_noticebase",v.rankType,v.rankSubType)
        if cfg then
            local r = logicMgr.ItemManager.ParseRewards(cfg.f_Reward)
            for k2,v2 in pairs(r) do
                if rewards[v2.itemModelId] then
                    rewards[v2.itemModelId].num = rewards[v2.itemModelId].num + v2.num
                else
                    rewards[v2.itemModelId] = v2
                end
            end
        end
    end
    local list = {}
    for k,v in pairs(rewards) do
        table.insert(list,v)
    end
    if #list == 0 then
        return
    end
    logicMgr.UIShowManager.ShowReward(list,nil,5)
end
--自动领取异动奖励(已废弃）
function t.ResChangeAwardHandler(msg)
    --msg = {state = 0,changeAward ={{cfgId = 1,state = 1},{cfgId = 7,state = 1},{cfgId = 13,state = 1}}}

    if msg.state ~= 0 then return end
end

------------------------------------七天排行榜-------开始------------
--请求排行榜
function t.ReqSevenRankList(rankType)
    local m = ReqSevenRankListMessage.New()
    m.rankType = rankType
    sendReqMessage(m)
end
--请求排行榜
function t.ResSevenRankList(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankServerDataByType(msg.rankType, msg.ranks, msg.round);
    logicMgr.NoticeManager.setSubTypeServerData(msg.subRank, msg.subRank2);
    logicMgr.NoticeManager.setSubSevenRankServerDataByType(msg.rankType, msg.subRanks);
    local tableData = logicMgr.NoticeManager.getSevenRankTableDataByType(msg.rankType)
    local day = logicMgr.NoticeManager.getSevenRankDay();
    if tableData and day and tableData.f_Day >= day then
        logicMgr.NoticeManager.setMySevenRankByType(msg.rankType, msg.rank);
    else
        logicMgr.NoticeManager.setMySevenRankByType(msg.rankType, msg.rank, msg.score);
    end
    logicMgr.NoticeManager.setMySevenRankReceiveLimit(msg.limitAmount);
    logicMgr.NoticeManager.setSevenRankSigninState(msg.signReward);
    --logicMgr.NoticeManager.updateSevenRankScore(msg.rankType, msg.score);
    FireEvent(Config.EventType.Fresh_SevenRank)
    FireEvent(Config.EventType.Fresh_SevenRankReward)
end

--领取全服限领奖励
function t.ReqSevenRankGetLimitReward(id)
    local m = ReqSevenRankGetLimitRewardMessage.New()
    m.tid = id
    sendReqMessage(m)
end
--领取全服限领奖励
function t.ResSevenRankGetLimitReward(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.updateSevenRankReceiveAward(msg.tid);
    logicMgr.NoticeManager.updateMySevenRankReceiveLimit(msg.tid, msg.amount);
    showUI(PanelNames.UICommonItemGet, nil, nil, 3, msg.items);
    FireEvent(Config.EventType.Fresh_SevenRankReward)
end

--- 领取签到奖励
function t.ReqSevenRankSignReward()
    local m = ReqSevenRankSignRewardMessage.New()
    sendReqMessage(m)
end

function t.GetSevenRankSignReward(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankSigninState(1);
    showUI(PanelNames.UICommonItemGet, nil, nil, 3, msg.items);
    FireEvent(Config.EventType.Fresh_SevenRankReward)
end

--排行榜信息推送
function t.ResSevenRankInfoPush(msg)
    dump(msg, "wwww", 10);
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankDay(msg.day, msg.round);
    local day = logicMgr.NoticeManager.getSevenRankDay();
    logicMgr.NoticeManager.setSevenRankReceiveAward(msg.rewards);
    logicMgr.NoticeManager.setMySevenRankReceiveLimit(msg.limitAmount);
    logicMgr.NoticeManager.setSevenRankScore(msg.modelScore);
    local myRank = 0;
    if msg.rank ~= nil then
        myRank = tonumber(tostring(msg.rank));
    end
    for k, v in pairs(msg.modelScore) do
        local tableData = logicMgr.NoticeManager.getSevenRankTableDataByType(v.type)
        if tableData and day and tableData.f_Day >= day then
            logicMgr.NoticeManager.setMySevenRankByType(v.type, myRank, v.score);
        end
    end
    if myRank < 1 then
        logicMgr.NoticeManager.reqSevenRankTimingUpdate();
    end
    logicMgr.NoticeManager.setSevenRankSigninState(msg.signReward);
    FireEvent(Config.EventType.Fresh_FuncMenuData)
end

--排行榜积分信息推送
function t.ResSevenRankScoreUpdate(msg)
    if msg.state ~= 0 then return end
    print(string.format("排行榜积分信息推送,类型:%s,分数:%s", msg.type, msg.score));
    logicMgr.NoticeManager.updateSevenRankScore(msg.type, msg.score)
    local tableData = logicMgr.NoticeManager.getSevenRankTableDataByType(msg.type)
    local day = logicMgr.NoticeManager.getSevenRankDay();
    if tableData and day and tableData.f_Day >= day then
        logicMgr.NoticeManager.setMySevenRankByType(msg.type, nil, msg.score)
    end
    t.ResSevenRankPlusScoreUpdate(msg);
end
------------------------------------七天排行榜-------结束------------

------------------------------------七天排行榜Plus-------开始------------
--- 领取阶段奖励
function t.ReqSevenRankGetStepReward(id)
    local m = ReqSevenRankGetStepRewardMessage.New()
    m.tid = id;
    sendReqMessage(m)
end

function t.GetSevenRankGetStepReward(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankPlusStepRewardState(nil, msg.tid);
    showUI(PanelNames.UICommonItemGet, nil, nil, 3, msg.items);
    FireEvent(Config.EventType.Fresh_SevenRankReward);
end
--排行榜信息推送
function t.GetSevenRank2InfoPush(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankPlusDay(msg.day);
    local day = logicMgr.NoticeManager.getSevenRankPlusDay();
    logicMgr.NoticeManager.setSevenRankPlusReceiveAward(msg.rewards);
    logicMgr.NoticeManager.setSevenRankPlusScore(msg.modelScore);
    local myRank = 0;
    if msg.rank ~= nil then
        myRank = tonumber(tostring(msg.rank));
    end
    for k, v in pairs(msg.modelScore) do
        local tableData = logicMgr.NoticeManager.getSevenRankPlusTableDataByType(v.type)
        if tableData and day and tableData.f_Day >= day then
            logicMgr.NoticeManager.setMySevenRankPlusByType(v.type, myRank, v.score);
        end
    end
    FireEvent(Config.EventType.Fresh_FuncMenuData)
end
--请求排行榜
function t.ReqSevenRankPlusList(rankType)
    local m = ReqSevenRank2ListMessage.New()
    m.rankType = rankType
    sendReqMessage(m)
end
--请求排行榜
function t.ResSevenRankPlusList(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.setSevenRankPlusServerDataByType(msg.rankType, msg.ranks);
    local tableData = logicMgr.NoticeManager.getSevenRankPlusTableDataByType(msg.rankType)
    local day = logicMgr.NoticeManager.getSevenRankPlusDay();
    if tableData and day and tableData.f_Day >= day then
        logicMgr.NoticeManager.setMySevenRankPlusByType(msg.rankType, msg.rank);
    else
        logicMgr.NoticeManager.setMySevenRankPlusByType(msg.rankType, msg.rank, msg.score);
    end
    FireEvent(Config.EventType.Fresh_SevenRank)
    FireEvent(Config.EventType.Fresh_SevenRankReward)
end
--排行榜积分信息推送
function t.ResSevenRankPlusScoreUpdate(msg)
    if msg.state ~= 0 then return end
    logicMgr.NoticeManager.updateSevenRankPlusScore(msg.type, msg.score)
    local tableData = logicMgr.NoticeManager.getSevenRankPlusTableDataByType(msg.type)
    local day = logicMgr.NoticeManager.getSevenRankPlusDay();
    if tableData and day and tableData.f_Day >= day then
        logicMgr.NoticeManager.setMySevenRankPlusByType(msg.type, nil, msg.score)
    end
    FireEvent(Config.EventType.Fresh_SevenRank);
end
------------------------------------七天排行榜Plus-------结束------------